from postfunctions import *
from collector import *
from BClassifier import *
from dbtrainer import *

import os
import datetime

if __name__=="__main__":
	
	import sys
	os.chdir("..")
	path=os.getcwd()
	sys.path += [path]
	os.environ['DJANGO_SETTINGS_MODULE'] = 'categories.settings'
	os.chdir("categories")

	updatetime = datetime.datetime.now()
	
	updateinfofile = open("update.info","w")
	updateinfo = str(updatetime.day)+"/"+str(updatetime.month)+"/"+str(updatetime.year)+" "+str(updatetime.hour)+":"+str(updatetime.minute)
	updateinfofile.write(updateinfo)
	updateinfofile.close()

	os.popen2("rm -f files/*")
	#os.popen2("rm -f files/*wordlisttrainedwords")
	#os.popen2("rm -f files/*wordlistsummary")

	c=Collector()
	lst = c.openxmlsrc("xml-resources-takip")
	
	for files in lst:
		#print files
		if files == ['']: continue
		src = c.parser(files)
		c.createfiles(src)
		c.createfiles(src,1)

	chkli = check()
	#print chkli
	c=BClassifier()
	c.summaryfilescreate(c,chkli)

	for x,y,z in os.walk("files"):
			lst=z
			i=0
			for f in lst:
			
				print i
				#print li
				if f.find("wordlistsummary") != -1 and chkli[f[:-15]] == True: pass
				else: continue
				i+=1
				print f
			
				#if i>20: break
				 
				t=DBtrainer()
				import time
				starttime=time.time() 
				category = t.calculateprobability("files/"+f)
				endtime = time.time()
				print "category time=", endtime-starttime
				girdiname = f.split("wordlistsummary")[0]+"girdi"
				girdi = open("files/"+girdiname,"r")
				fl=girdi.readlines()
				
				yzr=fl[0].strip()
				#print yzr
				bslk=fl[1].strip()
				#print bslk
				c=DBtrainer()
				trh=c.datefixer(fl[2].strip())
				#print trh
				lnk=girdiname.replace("--","/")
				lnk=lnk[:-5]
				#print lnk
				crk="".join(fl[3:])
				
				if len(category) == 0:
					print "digere girdi"
					something=__import__("categories.categoriesdb.models")
					clsname="diger"+"girdi"
					c=something.categoriesdb.models.__getattribute__(clsname)
					try:
						y=c(yazar=str(yzr), baslik=str(bslk), link=str(lnk), tarih=str(trh), icerik=str(crk))
						y.save()
					except:
						pass
				
				else:
					print "categorye girdi", category
					for h in category:
						something=__import__("categories.categoriesdb.models")
						clsname=h+"girdi"
						c=something.categoriesdb.models.__getattribute__(clsname)
						try:
							y=c(yazar=str(yzr), baslik=str(bslk), link=str(lnk), tarih=str(trh), icerik=str(crk))
							y.save()
						except:
							pass
			
				del fl # finished with fl list
				
				fname = open("files/"+f,"r")
				f=fname.readlines()
			
				for element in f:
							
					li = element.strip().split(" ")
					if len(li) == 1 or len(li) == 0: continue ## that is something like one keyword without a frequency
					for v in category:	
						clsname=v+"frekans"
						
					#print clsname	
						something=__import__("categories.categoriesdb.models")
						c=something.categoriesdb.models.__getattribute__(clsname)
						try:
							check=c.objects.get(kelime=li[0])
							value = int(check.frekans)+int(li[1])
							check.frekans=value
							check.save()
						except:
							y=c(kelime=li[0], frekans=int(li[1]))
							y.save()
				del f ## finished with f list			
				girdi.close()
				fname.close()
	print "bitti"